package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.pojo.CsMenu;
import cn.cs.pojo.CsMenuRole;
import cn.cs.pojo.CsRole;
import cn.cs.service.CsMenuService;
import cn.cs.service.CsRoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Resource
    private CsRoleService csRoleService;

    @Resource
    private CsMenuService csMenuService;

    /**
     * 分页查询 角色列表
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getRoleByPage")
    public Dto getRoleByPage(Integer pageNumber, Integer pageSize)throws Exception{
        Map<String, Object> param = new HashMap<>();
        return DtoUtil.returnSuccess("获取成功", csRoleService.queryCsRolePageByMap(param, pageNumber, pageSize));
    }

    /**
     * 查询所有角色列表
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getRoleAll")
    public Dto getRoleAll()throws Exception{
        return DtoUtil.returnSuccess("获取成功", csRoleService.getCsRoleListByMap(new HashMap<>()));
    }

    /**
     * 新增/编辑 角色
     * @param csRole
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateRole")
    public Dto updateRole(@RequestBody CsRole csRole){
        try {
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(csRole.getId())) {
                csRole.setId(IdWorker.getId());
                csRole.setCreateTime(new Date());
                csRole.setName("ROLE_"+ PingYinUtil.getFirstSpell(csRole.getRoleName()));
                csRoleService.addCsRole(csRole);
            } else {
                csRole.setModifyTime(new Date());
                msg = "修改成功";
                csRoleService.modifyCsRole(csRole);
            }
            return DtoUtil.returnSuccess(msg);
        }catch (Exception e){
            System.out.println(e.getCause().getMessage());
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 给角色授权
     * @param id
     * @param menus
     * @return
     */
    @RequestMapping(value = "/csPlatform/roleAuthorization")
    public Dto roleAuthorization(String id, String menus){
        try {
            List<CsMenuRole> list = new ArrayList<>();
            for(String m:menus.split(",")){
                CsMenuRole csMenuRole = new CsMenuRole();
                csMenuRole.setMenuId(m);
                csMenuRole.setRoleId(id);
                list.add(csMenuRole);
            }
            csRoleService.insertRoleMenusBatch(list, id);
            return DtoUtil.returnSuccess("授权成功");
        }catch (Exception e){
            System.out.println(e.getCause().getMessage());
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

    /**
     * 获取角色权限 资源树
     * @param id
     * @return
     */
    @RequestMapping(value = "/csPlatform/getRoleAuthorization")
    public Dto getRoleAuthorization(String id) {
        List<String> list = new ArrayList<>();
        if(EmptyUtils.isNotEmpty(id)){
            list.add(id);
        }
        List<CsMenu> menulist = csMenuService.getMenuList("0", new ArrayList<>(),list);
        for(CsMenu a:menulist){
            List<CsMenu> nextMenu = csMenuService.getMenuList(a.getId(), new ArrayList<>(),list);
            a.setNextMenu(nextMenu);
        }
        return DtoUtil.returnSuccess("操作成功",menulist);
    }

    /**
     * 获取角色权限 资源树
     * @param roles
     * @return
     */
    @RequestMapping(value = "/csPlatform/getRoleAuthorizationByRole")
    public Dto getRoleAuthorizationByRole(String roles, HttpServletRequest request) {
//        List<String> list = new ArrayList<>();
//        for(String role:roles.split(",")){
//            list.add(role);
//        }
//        return DtoUtil.returnSuccess("操作成功",csMenuService.getMenuTree(list, new ArrayList<>()));
        HttpSession session = request.getSession();
        Object list = session.getAttribute("menuList");
        if(EmptyUtils.isNotEmpty(list)){
            return DtoUtil.returnSuccess("操作成功", list);
        }else{
            return DtoUtil.returnFail("用户未登录!", "001001001");
        }

    }

    /**
     * 获取角色权限 资源列表
     * @param id
     * @return
     */
    @RequestMapping(value = "/csPlatform/getMenuByRoleId")
    public Dto getMenuByRoleId(String id) {
        return DtoUtil.returnSuccess("操作成功",csMenuService.getMenuByRoleId(id));
    }

    /**
     * 改变角色有效状态 1有效 0失效
     * @param id
     * @param state
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateRoleState")
    public Dto updateRoleState(String id, String state){
        try {
            CsRole csRole = new CsRole();
            csRole.setId(id);
            csRole.setRoleValidState(Integer.valueOf(state));
            return DtoUtil.returnSuccess("操作成功", csRoleService.modifyCsRole(csRole));
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败","001");
        }
    }

    /**
     * 删除角色
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delRole")
    public Dto delRole(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += csRoleService.deleteCsRoleById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("删除失败，存在员工，无法删除", "001");
        }
    }



    /**
     * 获取角色权限 资源树
     * @param roles
     * @return
     */
    @RequestMapping(value = "/csPlatform/getRoleAuthorizationAll")
    public Dto getRoleAuthorizationAll(String roles) {
        List<String> list = new ArrayList<>();
        list.add("ROLE_USER");
        List<CsMenu> menulist = csMenuService.getMenuListAll(list, new ArrayList<>());
        List<CsMenu> aa = new ArrayList<>();
        for(CsMenu a:menulist){
            if("0".equals(a.getMenuParentid())){
                List<CsMenu> bb = new ArrayList<>();
                for(CsMenu b:menulist){
                    if(a.getId().equals(b.getMenuParentid())){
                        List<CsMenu> cc = new ArrayList<>();
                        for(CsMenu c:menulist){
                            if(b.getId().equals(c.getMenuParentid())){
                                cc.add(c);
                            }
                        }
                        b.setNextMenu(cc);
                        bb.add(b);
                    }
                }
                a.setNextMenu(bb);
                aa.add(a);
            }
        }
        return DtoUtil.returnSuccess("操作成功",aa);
    }
}
